<template>
  <div class="con">
    <el-container>
      <!-- <el-header style="padding:0;height: 56px;">
        <headbox ref="mychild"></headbox>
      </el-header> -->
      <!-- <el-container>
        <el-aside  style="width:200px;background-color:#393D49;">
          <left></left>
        </el-aside>
      <el-container> -->
    <el-main style="padding:0;">
    <!-- 充值提现 -->
    <div class="contbox6_10">
      <p class="titlelist6_10">
        系统设置
        <span style="color: #999;">&nbsp;&nbsp;/&nbsp;&nbsp;</span>用户管理
      </p>
      <div class="content">
      <div class="sousuojg6_10">
        <el-form  :model="formInline" class="demo-form-inline">
          <el-form-item label="登录账户" label-width="90px" class="status">
            <el-input v-model="formInline.loginName" placeholder="请输入登录账户"></el-input>
          </el-form-item>
          <el-form-item label="用户名称" label-width="90px" class="status">
            <el-input v-model="formInline.name" placeholder="请输入用户名称"></el-input>
          </el-form-item>
          <el-form-item label="邮箱"  label-width="90px" class="status">
            <el-input v-model="formInline.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
          <el-form-item label="登录状态" class="status"  label-width="90px">
            <el-select v-model="formInline.loginFlag" clearable placeholder="请选择">
              <el-option label="全部" value=""></el-option>
              <el-option label="是" value="0"></el-option>
              <el-option label="否" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
          <el-button  v-if="isAuth('userSearch')" type="primary" @click="queryPayActiveLists" style="margin-right:10px">搜索</el-button>
          <el-button v-if="isAuth('userReset')" @click="result" >重置</el-button>
        <el-button v-if="isAuth('userAdd')" icon="el-icon-plus" type="primary" class="butt" style="margin-left:40px"  @click="add(1)">添加用户</el-button>
      </div>
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F9FAFC', color: '#222' }"
        stripe
        border
        :row-style="{ height: '0px' }"
        :cell-style="{ padding: '0px' }"
        v-loading="listLoading"
        :fit="true"
      >
        <el-table-column show-overflow-tooltip prop="loginName" min-width="110" label="登录账户"></el-table-column>
        <!-- <el-table-column show-overflow-tooltip prop="no" min-width="120" label="工号"></el-table-column> -->
        <el-table-column show-overflow-tooltip prop="name" min-width="100" label="姓名"></el-table-column>
        <el-table-column show-overflow-tooltip prop="email" min-width="180" label="邮箱"></el-table-column>
        <el-table-column show-overflow-tooltip prop="phone" min-width="110" label="手机号"></el-table-column>
        <el-table-column show-overflow-tooltip min-width="80" label="用户类型">
          <template slot-scope="scope">
            <!-- <span v-if='scope.row.userType == 1'>系统管理员</span> -->
            <span v-if='scope.row.userType == 1'>普通用户</span>
          </template>
        </el-table-column>
         <el-table-column show-overflow-tooltip prop="roleName" min-width="150" label="使用角色"></el-table-column>
        <!-- <el-table-column show-overflow-tooltip prop="loginIp" min-width="100" label="登录IP"></el-table-column> -->
        <el-table-column min-width="100" label="登录状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.loginFlag"
              active-color="#56BB8A"
              @change='saveUpdateRole(scope.row.id,scope.row.loginFlag)'
              inactive-color="#C0C4CC">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" min-width="160" label="创建时间"></el-table-column>
        <el-table-column min-width="200" label="操作">
          <template slot-scope="scope">
            <el-button size="text" v-if="isAuth('userEditor')" @click="add(2,scope.row.id)">修改</el-button>
            <el-button type="text" v-if="isAuth('userAssignPermissions')" @click="queryRoleByMenu(scope.row.id)" size="mini">角色分配</el-button>
            <!-- <el-button type="primary" v-if='scope.row.status == "1"' @click="updateStatus(scope.row.id,0)" size="mini">启用</el-button>
            <el-button type="danger" v-if='scope.row.status == "0"' @click="updateStatus(scope.row.id,1)" size="mini">停用</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align: right;"
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-size="formInline.number"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 50,100]"
        :total="totalNum"
      ></el-pagination>
    </div></div>
    </el-main>

    <!-- 添加/编辑 -->
    <addUser :addUserShow="addUserShow" :addUserType='addUserType' :addUserId='addUserId' v-on="addUserSuccess()"></addUser>

    <!-- 分配权限 -->
    <roleAssignment :roleAssignmentShow="roleAssignmentShow" :roleAssignmentType='roleAssignmentType' :roleAssignmentId='roleAssignmentId' v-on="roleAssignmentSuccess()"></roleAssignment>

    <!-- <el-footer style="padding:0;">
      <footerbox></footerbox>
    </el-footer> -->
        </el-container>
      <!-- </el-container>
    </el-container> -->
  </div>
</template>
<script type="text/ecmascript-6">
import bus from '../../assets/js/bus'
import https from '../../plugins/https'
import addUser from './addUser'
import roleAssignment from './roleAssignment'
import qs from 'qs'
export default {
  data() {
    return {
      formInline: {
        page: 1,
        number: 20,
        loginName: '',
        name: '',
        email: '',
        loginFlag: '',
        // oemUid: localStorage.getItem('jurisdiction') == 0 ? localStorage.getItem('uid') : '',
        officeUid: localStorage.getItem('uid')
      },
      addUserShow: false,
      roleAssignmentShow: false,
      roleAssignmentType: 2,
      dataTime: [],
      totalNum: 0, //总页数
      form: {},
      tableData: [],
      addUserType: 0,
      from:false,
      addUserId: 0,
      roleAssignmentId: 0,
      form: false
    };
  },
  components: {
    addUser,
    roleAssignment
  },
  mounted () {
    this.queryPayActiveList()
  },
  methods: {
    result(){
      this.formInline.loginName = "";
      this.formInline.name = ""
      this.formInline.email = ""
      this.formInline.page=1
      this.queryPayActiveList()
      // alert("点击了")
    },
    saveUpdateRole (id,status) {
      status = (status == '0' ? '1' : '0')
      this.listLoading = true
      let pl = {
        id: id,
        status: status
      }
      https.userUpdateStatus(pl).then(res => {
        if (res.data.code !== '0000') {
          layer.open({
            content: res.data.msg,
            skin: "msg",
            time:  2 //1秒后自动关闭
          });
          // this.$notify({title: "错误",message: res.data.msg,type: "error"});
          this.listLoading = false
          return
        }
        layer.open({
            content: res.data.msg,
            skin: "msg",
            time:  2 //1秒后自动关闭
          });
        // this.$notify({title: "成功",message: res.data.msg,type: "success"});
        this.queryPayActiveList()
        this.listLoading = false
      })
    },
    // 编辑
    add (type, addUserId) {
      this.addUserShow = true
      this.addUserType = type
      if (type == 2) this.addUserId = addUserId
    },
    // 角色分配
    queryRoleByMenu (id) {
      this.roleAssignmentShow = true
      this.roleAssignmentId = id
    },
    // 列表
    queryPayActiveList () {
      if (this.form == true) {
        return
      }
      this.form = true
      this.listLoading = true
      https.userList(this.formInline).then(res => {
        if (res.data.code !== '0000') {
          layer.open({
            content: res.data.msg,
            skin: "msg",
            time:  2 //1秒后自动关闭
          });
          // this.$notify({title: "错误",message: res.data.msg,type: "error"});
          this.listLoading = false
          this.form = false
          return
        }
        this.tableData = res.data.data
        for (let i = 0; i < this.tableData.length; i++) {
          this.tableData[i].loginFlag = this.tableData[i].loginFlag == '0' ? true : false
        }
        this.totalNum = res.data.total
        this.listLoading = false
        this.form = false
      })
    },
    handleSizeChange(val) {
      this.formInline.page = 1; //动态改变
      this.formInline.number = val; //动态改变
      this.queryPayActiveList()
    },
    handleCurrentChange(val) {
      this.formInline.page = val; //动态改变
      this.queryPayActiveList()
    },
    queryPayActiveLists(){
      this.formInline.page = 1; //动态改变
      this.queryPayActiveList()
    },
    //编辑/添加 
    addUserSuccess() {
      bus.$on("addUserSuccess", res => {
        this.addUserShow = res.show;
        if (res.type == 1) {
          this.queryPayActiveList()
        }
      });
    },
    roleAssignmentSuccess() {
      bus.$on("roleAssignmentSuccess", res => {
        this.roleAssignmentShow = res.show;
        this.queryPayActiveList()
      });
    },
  }
};
</script>
<style scoped>
.elbtn{
  float: right;
  margin: 20px;
}
.img {
  display: block;
  width: 100%;
}
.money p {
  width: 100%;
  line-height: 30px;
}
.status {
  font-size: 14px;
  margin-bottom: 20px;
}
.color {
  color: #e6a23c;
}
.mt20 {
  margin-top: 20px;
}
.shopList {
  width: 100%;
  border-bottom: 1px solid #ccc;
  line-height: 80px;
  font-size: 16px;
}
.formItem {
  float: left;
}
.pagination {
  margin-top: 10px;
}
.btnExport {
  float: left;
  margin-top: 10px;
  margin-bottom: 10px;
}
.btn {
  float: right;
}
.titlelist6_10 {
  width: 100%;
  float: left;
  font-size: 16px;
  color: #222;
  margin-top: 40px;
}
.con {
  position: relative;
}

.contbox6_10 {
  width: 100%;
  /*  height: 87.5vh; */
  overflow-x: auto;
  box-sizing: border-box;
  float: left;
}
.buttons > .el-button{
  width: 100px;
  float: left;
  height: 32px;
}
.sousuojg6_10{
  width: 100%
}
</style>
